package com.frskycockpit.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.os.Environment;

public class LogData {
	private Context mContext;
	private String filename;
	public static final String DELIMETER = ";";

	public LogData(Context ctx) {
		this.mContext = ctx;
		
		Date today = Calendar.getInstance().getTime();
		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		filename = "log_"+ simpleFormat.format(today) + ".txt";
	}

	public boolean isExternalStorageWritable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	public void append(String str) {
		File root = android.os.Environment.getExternalStorageDirectory();
		File dir = new File(root.getAbsolutePath() + "/FrskyCockpit");
		dir.mkdirs();
		File file = new File(dir, filename);

		String line = System.currentTimeMillis() + DELIMETER + str;
		
		try {
			FileOutputStream f = new FileOutputStream(file, true);
			PrintWriter pw = new PrintWriter(f);
			pw.println(line);
			pw.flush();
			pw.close();
			f.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
